package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctOutControlBo;
import com.rzdata.poct.system.domain.business.po.PoctMaterial;
import com.rzdata.poct.system.domain.business.po.PoctOutControl;
import com.rzdata.poct.system.domain.business.vo.PoctMaterialVo;
import com.rzdata.poct.system.domain.business.vo.PoctOutControlVo;
import com.rzdata.poct.system.mapper.PoctOutControlMapper;
import com.rzdata.poct.system.service.IPoctOutControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * poct 质控失控Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctOutControlServiceImpl extends ServicePlusImpl<PoctOutControlMapper, PoctOutControl, PoctOutControlVo> implements IPoctOutControlService {

    @Autowired
    private PoctOutControlMapper poctOutControlMapper;

    @Override
    public PoctOutControlVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctOutControlVo> queryPageList(PoctOutControlBo bo) {
        PagePlus<PoctOutControl, PoctOutControlVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctOutControlVo> queryList(PoctOutControlBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctOutControl> buildQueryWrapper(PoctOutControlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctOutControl> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getQualityId() != null, PoctOutControl::getQualityId, bo.getQualityId());
        lqw.eq(StringUtils.isNotBlank(bo.getHandler()), PoctOutControl::getHandler, bo.getHandler());
        lqw.eq(bo.getReson() != null, PoctOutControl::getReson, bo.getReson());
        lqw.eq(StringUtils.isNotBlank(bo.getAnalysis()), PoctOutControl::getAnalysis, bo.getAnalysis());
        lqw.eq(StringUtils.isNotBlank(bo.getCorrect()), PoctOutControl::getCorrect, bo.getCorrect());
        lqw.eq(bo.getType() != null, PoctOutControl::getType, bo.getType());
        lqw.eq(bo.getDeptId() != null, PoctOutControl::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), PoctOutControl::getDeptName, bo.getDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getLeader()), PoctOutControl::getLeader, bo.getLeader());
        lqw.eq(bo.getProcessTime() != null, PoctOutControl::getProcessTime, bo.getProcessTime());
        lqw.like(StringUtils.isNotBlank(bo.getInstrumentNo()), PoctOutControl::getInstrumentNo, bo.getInstrumentNo());
        lqw.like(StringUtils.isNotBlank(bo.getInstrumentName()), PoctOutControl::getInstrumentName, bo.getInstrumentName());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctOutControlBo bo) {
        PoctOutControl add = BeanUtil.toBean(bo, PoctOutControl.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctOutControlBo bo) {
        PoctOutControl update = BeanUtil.toBean(bo, PoctOutControl.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctOutControl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public TableDataInfo<PoctOutControlVo> queryPageOutList(PoctOutControl poctOutControl) {
        return PageUtils.buildDataInfo(poctOutControlMapper.queryPageOutList(PageUtils.buildPage(), poctOutControl));
    }
}
